package xyz.lwm.lazycat.beans;

import java.util.ArrayList;
import java.util.List;

/**
 * Bean ref 筛选条件
 *
 * @author lwm
 */
class BeanRefFilters implements BeanRefFilter {

    /**
     * or 筛选条件
     */
    private final List<BeanRefFilter> orPredicates = new ArrayList<>();

    /**
     * 添加筛选条件
     *
     * @param beanRefFilter 筛选条件
     */
    public void add(BeanRefFilter beanRefFilter) {
        orPredicates.add(beanRefFilter);
    }

    /**
     * 测试 BeanRef 是否满足条件, 如果无 filter, 则返回 true;
     * 否则返回 orPredicates 中任意一个 filter 返回 true
     *
     * @param beanRef BeanRef
     * @return 是否满足条件
     */
    @Override
    public boolean test(BeanRef<?> beanRef) {
        return orPredicates.isEmpty() || orPredicates.stream()
                .anyMatch(predicate -> predicate.test(beanRef));
    }

}
